﻿Imports System.Runtime.CompilerServices
Imports System.Text

Module Globals

    'Public HeaderColor As Color = Color.FromArgb(208, 224, 192)
    Public HeaderColor As Color = Color.FromArgb(192, 192, 192)
    Public GridColor As Color = Color.FromArgb(144, 160, 192)

    Public HelpMenu As ContextMenuStrip

    <Extension()>
    Function Resize(ByVal Image As Image, ByVal x As Integer, ByVal y As Integer, Optional ByVal m As Integer = 0) As Bitmap
        Dim NewImage As New Bitmap(x, y)
        Dim G As Graphics = Graphics.FromImage(NewImage)
        G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        G.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        G.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
        G.DrawImage(Image, New Rectangle(m, m, x - m * 2, y - m * 2), New Rectangle(0, 0, Image.Width, Image.Height), GraphicsUnit.Pixel)
        Return NewImage
    End Function

    <Extension()>
    Function IncColors(ByVal Image As Bitmap, ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As Image
        For x = 0 To Image.Width - 1
            For y = 0 To Image.Height - 1
                Dim c = Image.GetPixel(x, y)
                If c.A > 0 Then
                    Image.SetPixel(x, y, Color.FromArgb(c.A, Math.Max(0, Math.Min(c.R + r, 255)), Math.Max(0, Math.Min(c.G + g, 255)), Math.Max(0, Math.Min(c.B + b, 255))))
                End If
            Next
        Next
        Return Image
    End Function

    Function MakeImage(ByVal Image1 As Image, ByVal Image2 As Image, ByVal x As Integer, ByVal y As Integer, Optional ByVal m As Integer = 0) As Image
        Dim NewImage As New Bitmap(x, y)
        Dim G As Graphics = Graphics.FromImage(NewImage)
        G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        G.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        G.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
        G.DrawImage(Image1, New Rectangle(m, m + y \ 4, x * 3 \ 4 - m * 2, y * 3 \ 4 - m * 2), New Rectangle(0, 0, Image1.Width, Image1.Height), GraphicsUnit.Pixel)
        G.DrawImage(Image2, New Rectangle(x \ 2, 0, x \ 2, y \ 2), New Rectangle(0, 0, Image2.Width, Image2.Height), GraphicsUnit.Pixel)
        Return NewImage
    End Function


    Function FormatGun(ByVal x As Decimal) As String
        Return If(x = 0, "", Trim(x))
    End Function

    Function FormatSaat(ByVal x As Decimal) As String
        Return Format(Int(x), "00") & ":" & Format((x - Int(x)) * 60, "00")
    End Function

    Function FormatIzin(ByVal x As Decimal, ByVal y As Integer) As String
        Return If(x = 0 And y = 0, "", x.ToString("##0.0") & " (" & y.ToString("00") & ")")
    End Function

End Module

Module Extensions

    <Extension()> _
    Public Function YMD(ByVal s As String) As String
        Return Mid(s, 7, 4) & Mid(s, 4, 2) & Mid(s, 1, 2)
    End Function

    <Extension()> _
    Public Function Clone(ByVal m As ToolStripMenuItem) As ToolStripMenuItem
        Dim Menu As New ToolStripMenuItem
        Menu.Text = m.Text
        Menu.Image = m.Image
        AddHandler Menu.Click, AddressOf m.PerformClick
        For Each SubItem In m.DropDownItems
            If TypeOf SubItem Is ToolStripMenuItem Then
                Menu.DropDownItems.Add(Clone(SubItem))
            ElseIf TypeOf SubItem Is ToolStripSeparator Then
                Menu.DropDownItems.Add("-")
            End If
        Next
        Return Menu
    End Function

    <Extension()> _
    Public Function Clone(ByVal m As ToolStripSplitButton) As ToolStripMenuItem
        Dim Menu As New ToolStripMenuItem
        Menu.Text = m.Text
        Menu.Image = m.Image
        AddHandler Menu.Click, AddressOf m.PerformClick
        For Each SubItem In m.DropDownItems
            If TypeOf SubItem Is ToolStripMenuItem Then
                Menu.DropDownItems.Add(Clone(SubItem))
            ElseIf TypeOf SubItem Is ToolStripSeparator Then
                Menu.DropDownItems.Add("-")
            End If
        Next
        Return Menu
    End Function

	<Extension()> _
	Function ToTitle(ByVal Text As String) As String
		If Text.Trim = "" Then Return ""
		Text = " " & Text.ToLower
		Dim Pos As Integer = 0
		Do
			Pos = InStr(Pos + 1, Text, " ")
			If Pos = 0 Then Exit Do
			If Pos = Text.Length Then Exit Do
			Mid(Text, Pos + 1, 1) = Mid(Text, Pos + 1, 1).ToUpper
		Loop
		Return Text.Trim
	End Function

    <Extension()> _
    Function Tatil(ByVal d As Date) As Boolean
        Static Dates As New SortedList(Of Date, Boolean)
        If Not Dates.ContainsKey(d) Then Dates.Add(d, Service.Tatil(d))
        Return Dates(d)
    End Function

    Function SaatHesapla(ByVal Saat1 As String, ByVal Saat2 As String) As Decimal
        If Saat1 = "" OrElse Saat2 = "" Then Return 0
        If Len(Saat1) = 4 Then
            Return Mid(Saat2, 1, 2) - Mid(Saat1, 1, 2) + Mid(Saat2, 3, 2) / 60 - Mid(Saat1, 3, 2) / 60
        Else
            Return Mid(Saat2, 1, 2) - Mid(Saat1, 1, 2) + Mid(Saat2, 4, 2) / 60 - Mid(Saat1, 4, 2) / 60
        End If
    End Function

    <Extension()> _
    Sub SetColors(ByVal Style As DataGridViewCellStyle, ByVal BackColor As Color, ByVal ForeColor As Color)
        Style.BackColor = BackColor
        Style.SelectionBackColor = BackColor.Lighter(0.4)
        Style.ForeColor = ForeColor
        Style.SelectionForeColor = ForeColor.Lighter(0.4)
    End Sub

    <Extension()> _
    Function Lighter(ByVal c As Color, ByVal percent As Double) As Color
        Return Color.FromArgb(c.R * (1 - percent) + 255 * percent, c.G * (1 - percent) + 255 * percent, c.B * (1 - percent) + 255 * percent)
    End Function

    <Extension()> _
    Function Darker(ByVal c As Color, ByVal percent As Double) As Color
        Return Color.FromArgb(c.R * (1 - percent), c.G * (1 - percent), c.B * (1 - percent))
    End Function

    Function NoLog() As Boolean
        Return My.Computer.Name.ToLower = "ssd-ahmet"
    End Function

End Module

